package com.huaxin.hxcloud.work;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.FrameLayout;

import com.huaxin.common.BaseActivity;
import com.huaxin.common.HxAnalysisDTO;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.common.utils.HxUtils;
import com.huaxin.hxcloud.R;
import com.huaxindata.common.util.HxLogUtils;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author songxf
 * @date 2018/6/13
 * @Desc SPT运维统计分析图表
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxSPTStatisticsActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar     mToolbar;
    @BindView(R.id.web_view)
    WebView     mWebView;
    @BindView(R.id.content)
    FrameLayout mContent;
    private ArrayList<String> mMaintenaceList;
    private String            mMaintenaceDate;
    private static final String              TAG              = "==HxSPTStatisticsActivity==";
    //巡检站点数组
    final                List<List<String>>  mStationNumList  = new ArrayList<>();
    final                List<String>        mLegendList      = new ArrayList<>();
    //已办待办工单数组
    final                List<List<Integer>> mWorkTaskNumList = new ArrayList<>();
    private String        mToToDoPercent;
    private HxAnalysisDTO mAnalysisDTO;

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.act_spt_statistics;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
     /*   intent.putStringArrayListExtra("MAINTENACE_LIST", mSelectList);
        intent.putExtra("MAINTENACE_DATE", mSelectDate);*/
        mMaintenaceList = extras.getStringArrayList("MAINTENACE_LIST");
        mMaintenaceDate = extras.getString("MAINTENACE_DATE");
    }

    @Override
    public void initView() {
        HxLogUtils.i(TAG + "选择集合" + mMaintenaceList);
        HxLogUtils.i(TAG + "选择日期" + mMaintenaceDate);
        mToolbar.setTitle("运维分析");
        setSupportActionBar(mToolbar);
        initWebView();
    }


    private void initWebView() {
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        mWebView.loadUrl("file:///android_asset/maintenance.html");
        updateLoadStatus(LOADING);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                return super.shouldOverrideUrlLoading(webView, s);
            }

            @Override
            public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
                super.onPageStarted(webView, s, bitmap);
            }

            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                if (!mAnalysisDTO.isSuccessful()) {
                    updateLoadStatus(LOAD_FAILURE);
                    return;
                }
                setDataEchartView(mLegendList.toString(), mStationNumList.toString(), mWorkTaskNumList.toString(), mToToDoPercent);
            }
        });
    }

    @Override
    protected void requestData() {
        /*巡检站点数量：stationNum
        任务完成数量：workTaskNum
        已办待办工单：arrangeTask*/
        Call<HxAnalysisDTO> call = HxTransmitRequestHelper.getRequestApi().getSPTAnalysis(mMaintenaceDate, HxUtils.getTruncationStr(mMaintenaceList.toString()));
        HxLogUtils.i(TAG + "选择集合--->" + HxUtils.getTruncationStr(mMaintenaceList.toString()));
        autoClose(call);
        call.enqueue(new Callback<HxAnalysisDTO>() {
            @Override
            public void onResponse(Call<HxAnalysisDTO> call, Response<HxAnalysisDTO> response) {
                mAnalysisDTO = response.body();
                if (response.isSuccessful() && mAnalysisDTO.isSuccessful()) {
                    HxAnalysisDTO.DataBean dataBean = mAnalysisDTO.getData();
                    List<HxAnalysisDTO.DataBean.StationNumBean> stationNum = dataBean.getStationNum();
                    //任务完成数量
                    List<HxAnalysisDTO.DataBean.WorkTaskNumBean> workTaskNum = dataBean.getWorkTaskNum();
                    for (int i = 0; i < stationNum.size(); i++) {
                        HxAnalysisDTO.DataBean.StationNumBean stationNumBean = stationNum.get(i);
                        List<String> data = stationNumBean.getData();
                        mStationNumList.add(data);
                        mLegendList.add("\"" + stationNumBean.getName() + "\"");
                    }
                    for (int i = 0; i < workTaskNum.size(); i++) {
                        HxAnalysisDTO.DataBean.WorkTaskNumBean workTaskNumBean = workTaskNum.get(i);
                        List<Integer> data = workTaskNumBean.getData();
                        mWorkTaskNumList.add(data);
                    }
                    HxAnalysisDTO.DataBean.DoneAndToDoWorkTaskBean toDoWorkTask = dataBean.getDoneAndToDoWorkTask();
                    NumberFormat numberFormat = NumberFormat.getInstance();
                    // 设置精确到小数点后2位
                    numberFormat.setMaximumFractionDigits(1);
                    //待完成
                    Integer todoWarkTask = Integer.parseInt(toDoWorkTask.getDone());
                    //已完成
                    Integer doneWarkTask = Integer.parseInt(toDoWorkTask.getTodo()) + todoWarkTask;
                    mToToDoPercent = numberFormat.format((float) todoWarkTask / (float) doneWarkTask);
                    if (0 == todoWarkTask && 0 == doneWarkTask) {
                        mToToDoPercent = "0";
                    }
                } else {
                    updateLoadStatus(LOAD_FAILURE);
                }
            }

            @Override
            public void onFailure(Call<HxAnalysisDTO> call, Throwable t) {
                updateLoadStatus(LOAD_FAILURE);
            }
        });
    }

    private void setDataEchartView(String legendStr, String stationNum, String legendTwoStr, String progress) {
        updateLoadStatus(LOAD_SUCCESS);
        String body = "'{\"first\":{\"title\":\"巡检站点数量\",\"legend\":{\"data\":" + legendStr + "},\"series\":" + stationNum + "}," +
                "\"second\":{\"title\":\"任务完成数量\",\"legend\":{\"data\":" + legendStr + "},\"series\":" + legendTwoStr + "}," +
                "\"progress\":" + progress + "}'";
        HxLogUtils.i("数据--->" + body);
        mWebView.loadUrl("javascript:drawChart(" + body + ")");
    }
}
